package com.sram.action;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.RequestUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.opensymphony.xwork2.ActionContext;
import com.sram.entity.Photo;

import com.sram.util.DateUtil;
import com.sram.util.DirectoryUtil;
import com.sram.util.ImageUtil;
import com.sram.util.JSONUtils;
public class UpdownAction2 extends BaseAction{
	protected File fileData;//和html中input  name一致
	protected String fileDataFileName;
	protected File destFile;
	//使用uoloadflay插件，在后台获取不到session值，用？upload2.action?username=${sessionScope.username}
	protected String  username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public File getFileData() {
		return fileData;
	}

	public void setFileData(File fileData) {
		this.fileData = fileData;
	}

	public String getFileDataFileName() {
		return fileDataFileName;
	}

	public void setFileDataFileName(String fileDataFileName) {
		this.fileDataFileName = fileDataFileName;
	}

	public File getDestFile() {
		return destFile;
	}

	public void setDestFile(File destFile) {
		this.destFile = destFile;
	}

	public String upload2() throws Exception{
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			String path = DirectoryUtil.geneRadomDir(fileDataFileName);
			destFile = new File(ServletActionContext.getServletContext().getRealPath("/upload")+"/"+path);
			String newFileName = DirectoryUtil.uuidName(fileDataFileName);
			// 目标路径
			String url = "/upload" + path + "/" + newFileName;
			if(!destFile.exists()){
				destFile.mkdirs();
			}
			File descFile = new File(destFile, newFileName);
			System.out.println("........................");
			System.out.println(fileData);
			System.out.println(fileDataFileName);
			System.out.println(destFile);

			// 保存服务器地址(大图)
			FileUtils.copyFile(fileData, descFile);
			
			String samllPath = null;
			Integer[] picSize = new Integer[] { 0, 0 };
			
			samllPath = ServletActionContext.getServletContext().getRealPath("/upload/small") + path;
			String samllurl =  "/upload/small" + path + "/" + newFileName;
			File samlldestFile = new File(samllPath);
			if(!samlldestFile.exists()){
				samlldestFile.mkdirs();
			}
			File smallFile = new File(samlldestFile,newFileName);
			System.out.println(samllPath);
			System.out.println(samllurl);
			// 保存服务器地址(小图100x100)
			FileUtils.copyFile(fileData, smallFile);
			picSize = ImageUtil.generateSmallImg(descFile.getAbsolutePath(), smallFile.getAbsolutePath());
			//保存数据库
			Photo photo=new Photo();
			
			photo.setPhoto(url);
			photo.setUsername(username);
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			df.format(new Date());
			System.out.println(df.format(new Date()));
			photo.setTime(DateUtil.DateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));
			photo.setSmallphoto(samllurl);
			System.out.println(photo);
			
			photoService.savePhoto(photo);
			
			jsonMap.put("urlData", url);
			jsonMap.put("smallUrlData",samllurl);
			jsonMap.put("savephoto", "save");
		} catch (Exception e) {
			jsonMap.put("errorMsg", "服务器繁忙，请重试！");
		}
		return JSONUtils.jsonOut(jsonMap);
	 }
	public String user_photo() throws Exception{
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			String nn=(String) this.getSession().getAttribute("username");
			DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Photo.class);
			detachedCriteria.add(Restrictions.eq("username", nn));
			//detachedCriteria.add(Restrictions.isNotNull("spSspic1"));
			//按时间排序
			detachedCriteria.addOrder(Order.desc("time"));
			List<Photo> photos = photoService.findPhoto(detachedCriteria);
			int pageNo = Integer.parseInt(this.getRequest().getParameter("pageNo"));
			//每页显示的数量
			int pageSize = 5;
			//每页第一个的位置
			int firstResult = (pageNo - 1) * pageSize;
            //第几页的集合
			List<Photo> photos1=photoService.findByDetachedCriteriaLimit(detachedCriteria, firstResult, pageSize);
			//总共的页数
			int totals = photos.size();
			Long pages = (long) (totals % pageSize > 0 ? totals / pageSize + 1 : totals / pageSize);
			//总数
			Long total = (long) photos.size();
				
			jsonMap.put("photos", photos1);
			jsonMap.put("pages", pages);
			jsonMap.put("total", total);
			jsonMap.put("msg", "1");
	
		} catch (Exception e) {
			jsonMap.put("msg", "0");
		}
		return JSONUtils.jsonOut(jsonMap);
	}
}
